package com.example.mywhat2;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.mywhat2.adapter.MyFragmentPagerAdapter;
import com.example.mywhat2.fragment.BlankFragment;

import java.util.ArrayList;

//jetpack改变android开发方式
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ViewPager2 viewPager2=null;
    private MyFragmentPagerAdapter adapter =null;
    private ArrayList<Fragment> fragments =new ArrayList<>();
    private LinearLayout tab_l1,tab_l2,tab_l3,tab_l4;
    private ImageView tab_img1,tab_img2,tab_img3,tab_img4,imgCurrent;
    //viewinject可以让我们不用写这些代码
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViewPager();
        initTabView();
    }

    private void initTabView() {
        tab_l1 =findViewById(R.id.tabs_l1);
        tab_l2 =findViewById(R.id.tabs_l2);
        tab_l3 =findViewById(R.id.tabs_l3);
        tab_l4 =findViewById(R.id.tabs_l4);
        tab_img1 =findViewById(R.id.tabs_l1_img1);
        tab_img2 =findViewById(R.id.tabs_l2_img1);
        tab_img3 =findViewById(R.id.tabs_l3_img1);
        tab_img4 =findViewById(R.id.tabs_l4_img1);
        tab_l1.setOnClickListener(this);
        tab_l2.setOnClickListener(this);
        tab_l3.setOnClickListener(this);
        tab_l4.setOnClickListener(this);
        //设置默认选中
        tab_img1.setSelected(true);
        imgCurrent =tab_img1;
    }

    public void initViewPager(){
        viewPager2 =findViewById(R.id.viewpager);
        fragments.add(BlankFragment.newInstance("微信聊天"));
        fragments.add(BlankFragment.newInstance("通讯录"));
        fragments.add(BlankFragment.newInstance("发现"));
        fragments.add(BlankFragment.newInstance("我"));
        adapter =new MyFragmentPagerAdapter(getSupportFragmentManager(),getLifecycle(),fragments);
        viewPager2.setAdapter(adapter);
        //设置viewpager页面滑动的监听
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }
    //在页面滑动改变tab
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changeTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });

    }

    private void changeTab(int position) {
        imgCurrent.setSelected(false);
        switch (position){
            case R.id.tabs_l1:
                viewPager2.setCurrentItem(0);
            case 0:
                tab_img1.setSelected(true);
                imgCurrent =tab_img1;
                break;
            case R.id.tabs_l2:
                viewPager2.setCurrentItem(1);
            case 1:
                tab_img2.setSelected(true);
                imgCurrent =tab_img2;
                break;
            case R.id.tabs_l3:
                viewPager2.setCurrentItem(2);
            case 2:
                tab_img3.setSelected(true);
                imgCurrent =tab_img3;
                break;
            case R.id.tabs_l4:
                viewPager2.setCurrentItem(3);
            case 3:
                tab_img4.setSelected(true);
                imgCurrent =tab_img4;
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        changeTab(v.getId());



    }
}